﻿Imports SIRData
Imports System.IO
Imports Microsoft.Office.Interop
Public Class frmNotaDeIngresoNuevo
  'Private m_bKey As Boolean = False

  Private Sub frmNotaDeIngresoNuevo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
      Call ListarOrdenCompra()


      'm_bKey = True ' Establecer la llave

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub ListarOrdenCompra()
    Dim m_OrdenCompra As New clsOrdenCompra(g_sConnectString)
    Try

      dgvListado.DataSource = m_OrdenCompra.OrdenCompraListar()

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_OrdenCompra = Nothing
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvListado.RowPrePaint
    Try
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.BackColor = IIf(e.RowIndex Mod 2 = 0, Color.Honeydew, Color.White)
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.Font = New Font("Lucida Sans", 8)
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvListado.CellContentClick
    Try
      'Esta parte me permite seleccionar solo un checkbox y se elimina el anterior 
      dgvListado.Rows(e.RowIndex).Cells("chkSel").Value = Not dgvListado.Rows(e.RowIndex).Cells("chkSel").Value
      For Each fila As DataGridViewRow In dgvListado.Rows
        If Not fila Is dgvListado.Rows(dgvListado.CurrentCell.RowIndex) Then
          fila.Cells("chkSel").Value = False
        End If
      Next

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbrir.Click
    Try
      If dgvListado.Item("chkSel", dgvListado.CurrentCell.RowIndex).Value = False Then
        MessageBox.Show("Debe seleccionar con un check el registro que desea editar.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        OpenFileDialogPlan.Title = "Abrir Excel - Usuarios"
        OpenFileDialogPlan.Filter = "Todos los Archvos Excel|*.xlsx;*.xls"

        If OpenFileDialogPlan.ShowDialog() = DialogResult.OK Then
          lblRuta.Text = OpenFileDialogPlan.FileName
        End If

      End If
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Function ObtenerOrdenCompraDetalle(ByVal piNumOrdCom As Int32) As DataTable
    Dim m_OrdenCompraDetalle As New clsOrdenCompraDetalle(g_sConnectString)
    Try

      Return m_OrdenCompraDetalle.OrdenCompraDetalleListar(piNumOrdCom)

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_OrdenCompraDetalle = Nothing
      GC.Collect()
    End Try
  End Function

  Private Sub RecorrerPackingListXLS(ByVal ruta As String, ByVal odtOCDetalle As DataTable)
    Dim xlApp As New Excel.Application
    Dim xlLibro As Excel.Workbook
    Dim xlHoja1 As Excel.Worksheet

    Try
      xlApp.Visible = False
      xlLibro = xlApp.Workbooks.Open(ruta)
      xlHoja1 = xlLibro.Worksheets("Sheet1")

      ' Realizo la comparación
      For i As Integer = 0 To odtOCDetalle.Rows.Count - 1
        Dim cCodProd As String = xlHoja1.Range("B" & (19 + i).ToString).Text
        If odtOCDetalle.Rows(i)("cCodProd").ToString.Trim <> cCodProd.Replace("-", "").Trim Then
          Throw New Exception("Error en Comparación de Código Interno")
        End If
      Next

      xlLibro.Close(False)
    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      xlApp.Quit()
    End Try
  End Sub

  Dim frmWait As New WaitWindow

  Private Sub btnGenerar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerar.Click
    Dim dtOrdenCompraDetalle As New DataTable
    Try
      dtOrdenCompraDetalle = ObtenerOrdenCompraDetalle(dgvListado.Item("iNumOrdCom", dgvListado.CurrentCell.RowIndex).Value)
      Call RecorrerPackingListXLS(lblRuta.Text, dtOrdenCompraDetalle)
      Dim oForm As New frmNotaDeIngresoEditar

      oForm.bNuevo = True
      oForm.oiNumOrdCom = dgvListado.Item("iNumOrdCom", dgvListado.CurrentCell.RowIndex).Value
      oForm.ShowDialog()

      Me.Close()

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      dtOrdenCompraDetalle = Nothing
      GC.Collect()
    End Try
  End Sub

  Private Sub btnCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCerrar.Click
    Me.Close()
  End Sub

  Private Sub ProgressWorkerMethod(ByVal sender As Object, ByVal e As WaitWindowEventArgs)
    ' Do something
    For progress As Integer = 1 To 100
      System.Threading.Thread.Sleep(20)
      ' Update the wait window message
      e.Window.Message = String.Format("Por favor espere ... {0}%", progress.ToString.PadLeft(3))
    Next

    ' Use the arguments sent in
    If e.Arguments.Count > 0 Then
      ' Set the result to return
      e.Result = e.Arguments(0).ToString
    Else
      ' Set the result to return
      e.Result = "Hello Word"
    End If
  End Sub


End Class

'Dim myStream As Stream = Nothing
'Dim openFileDialog1 As New OpenFileDialog()
'    Try

'      openFileDialog1.InitialDirectory = "c:\"
'      openFileDialog1.Filter = "Excel Worksheets|*.xls"
''"Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" & "|Office Files|*.doc;*.xls;*.ppt" & "|All Files|*.*"
'      openFileDialog1.FilterIndex = 2
'      openFileDialog1.RestoreDirectory = True

'      If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
'        Try
'          myStream = openFileDialog1.OpenFile()
'          If (myStream IsNot Nothing) Then
'' Insert code to read the stream here.

'          End If
'        Catch Ex As Exception
''MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
'          MessageBox.Show(Ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
'        Finally
'' Check this again, since we need to make sure we didn't throw an exception on open.
'          If (myStream IsNot Nothing) Then
'            myStream.Close()
'          End If
'        End Try
'      End If

'    Catch ex As Exception
'      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
'    Finally
'      GC.Collect()
'    End Try